<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2021/5/17
 * Time: 12:12
 */

namespace app\common\model;

use think\Model;

class ChatMessage extends Model
{
    protected $name = "chat_message";

    public static function Record($params)
    {
        $data['uuid'] = $params['uuid'];
        $data['user_id'] = $params['user_id'];
        $data['company_id'] = $params['company_id'];
        $data['content'] = $params['content'];
        $data['type'] = 1;
        $data['msg_status'] = $params['msg_status'];
        $data['source_status'] = $params['source_status'];
        $data['add_time'] = time();
        self::create($data);
    }


    /**
     * 记录管理员信息
     * @param $params
     * @return mixed
     */
    public static function RecodeRet($params)
    {
        $data['uuid'] = $params['uuid'];
        $data['user_id'] = $params['user_id'];
        $data['company_id'] = $params['company_id'];
        $data['content'] = $params['content'];
        $data['source_status'] = $params['source_status'];
        $data['type'] = 1;
        $data['add_time'] = time();
        self::create($data);
        $id = self::getLastInsID();
        $type = $data['type'];
        return ['id' => $id, 'content' => $data['content'], 'type' => $type];
    }


    /**
     * 更新未读消息为已读
     * @param $uuid
     * @param $group_id
     * @param $source_status
     * @return $this
     */
    public static function updateMsg($uuid, $userId, $source_status = 1)
    {
        $where['uuid'] = $uuid;
        $where['user_id'] = $userId;
        $where['source_status'] = $source_status;
        return self::where($where)->update(['msg_status' => 2]);
    }

    public static function getVisitorList($userId,$companyId){
        $visitor_m = new Chatvisitor();
        $where['v.user_id'] = $userId;
        $where['v.company_id'] = $companyId;
        $where['m.add_time'] = ['between', [strtotime('-7 days'), time() + 3600]];
        $list = $visitor_m->alias('v')->field('v.*')
            ->join('jkb_chat_message m', 'm.uuid = v.uuid')
            ->where($where)->group('v.uuid')->order('v.status desc,m.add_time desc')->select();
//        echo $visitor_m->getLastSql();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $where_['uuid'] = $v['uuid'];
                $where_['company_id'] = $companyId;
                $where_['msg_status'] = 1;
                $where_['source_status'] = 1;
                $unreadCount = ChatMessage::where($where_)->count();
                $list[$k]['unread'] = $unreadCount ? $unreadCount : 0;
                unset($where_['msg_status']);
                unset($where_['source_status']);
                $msg = ChatMessage::where($where_)->order('add_time desc')->field('content,add_time,type,source_status')->find();
                if ($msg) {
                    $msg['time'] = date('H:i', $msg['add_time']);
                }
                $list[$k]['msg'] = $msg;
            }
        }
        return $list;
    }
}